package com.demo.youxuanmall.controller;

import com.demo.youxuanmall.entity.Address;
import com.demo.youxuanmall.entity.User;
import com.demo.youxuanmall.service.AddressService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/user/address")
@RequiredArgsConstructor
public class AddressController {

    private final AddressService addressService;

    @PostMapping("/add")
    public String addAddress(Address address, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }

        try {
            address.setUserId(user.getId());
            address.setIsDeleted(0);
            addressService.addAddress(address);
            log.info("用户 {} 添加收货地址成功", user.getUsername());
            return "redirect:/user/profile?active=address&success=true";
        } catch (Exception e) {
            log.error("添加收货地址失败", e);
            return "redirect:/user/profile?active=address&error=true";
        }
    }

    @PostMapping("/update")
    public String updateAddress(Address address, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }

        try {
            address.setUserId(user.getId());
            addressService.updateAddress(address);
            log.info("用户 {} 更新收货地址成功", user.getUsername());
            return "redirect:/user/profile?active=address&success=true";
        } catch (Exception e) {
            log.error("更新收货地址失败", e);
            return "redirect:/user/profile?active=address&error=true";
        }
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public String deleteAddress(@PathVariable Long id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "error";
        }

        try {
            addressService.deleteAddress(id);
            log.info("用户 {} 删除收货地址成功", user.getUsername());
            return "success";
        } catch (Exception e) {
            log.error("删除收货地址失败", e);
            return "error";
        }
    }

    @PostMapping("/set-default/{id}")
    @ResponseBody
    public String setDefaultAddress(@PathVariable Long id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            log.error("设置默认地址失败：用户未登录");
            return "error";
        }

        try {
            log.info("设置默认地址: userId={}, addressId={}", user.getId(), id);
            addressService.setDefaultAddress(user.getId(), id);
            log.info("用户 {} 设置默认地址成功", user.getUsername());
            return "success";
        } catch (Exception e) {
            log.error("设置默认地址失败: userId={}, addressId={}, error={}", user.getId(), id, e.getMessage(), e);
            return "error";
        }
    }
} 